package com.atbun.eduservice.controller;


import com.atbun.commonutils.R;
import com.atbun.eduservice.entity.EduCourse;
import com.atbun.eduservice.entity.vo.CourseInfoVo;
import com.atbun.eduservice.entity.vo.CoursePublishVo;
import com.atbun.eduservice.entity.vo.CourseQuery;
import com.atbun.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-05-06
 */
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {
    @Autowired
    private EduCourseService eduCourseService;

    @ApiOperation("新增课程")
    @PostMapping("saveCourseInfo")
    public R saveCourseInfo(
            @ApiParam(name = "CourseInfoForm", value = "课程基本信息", required = true)
            @RequestBody CourseInfoVo courseInfoVo){

        String courseId = this.eduCourseService.saveCourseInfo(courseInfoVo);
        if(!StringUtils.isEmpty(courseId)){
            return R.ok().data("courseId", courseId);
        }else{
            return R.error().message("保存失败");
        }
    }

    @ApiOperation(value = "根据ID查询课程信息")
    @GetMapping("courseInfo/{courseId}")
    public R getById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String courseId){

        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfoFormById(courseId);
        return R.ok().data("courseInfoVo", courseInfoVo);
    }


    @ApiOperation(value = "修改课程信息")
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        this.eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }


//***************************************发布时获取数据 开始***************************************//
    @ApiOperation(value = "获取发布课程信息")
    @GetMapping("getCoursePublishVoById/{id}")
    public R getCoursePublishVoById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id){
        CoursePublishVo coursePublishVo = this.eduCourseService.getCoursePublishVoById(id);
        return R.ok().data("courseInfoVo",coursePublishVo);
    }

    @ApiOperation(value = "发布课程信息")
    @PutMapping("publishCourse/{id}")
    public R publishCourse(
            @ApiParam(name = "id",value = "课程id",required = true)
            @PathVariable String id){
        boolean publishCourseById = this.eduCourseService.publishCourseById(id);
        if (publishCourseById){
            return R.ok();
        }
        return R.error();
    }
//***************************************发布时获取数据 结束***************************************//

    @ApiOperation(value = "课程分页查询列表")
    @PostMapping("getCoursePageList/{page}/{limit}")
    public R getCoursePageList(
            @ApiParam(name = "page",value = "当前页码",required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit",value = "每页记录数",required = true)
            @PathVariable Long limit,
            @ApiParam(name = "courseQuery",value = "每页记录数",required = true)
            @RequestBody CourseQuery courseQuery){ // 注意：GetMapper不能使用@RequestBody
//        可以先对分页进行封装
        Page<EduCourse> eduCoursePage = new Page<>(page,limit);
        this.eduCourseService.getCoursePageList(eduCoursePage,courseQuery);
        return R.ok().data("total",eduCoursePage.getTotal()).data("rows",eduCoursePage.getRecords());
    }

    @ApiOperation(value = "根据课程id删除课程，章节-小节，视频")
    @DeleteMapping("deleteCourse/{courseId}")
    public R deleteCourse(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId){
        boolean result = this.eduCourseService.deleteCourseAndTeacherAndVideoByCourseId(courseId);
        if (result){
            return R.ok();
        }
        return R.error().message("删除失败");
    }

}

